package LeetCode;



//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/4/18.
// 
public class 寻找两个正序数组的中位数 {
//    https://leetcode.cn/problems/median-of-two-sorted-arrays/description/
    public static void main(String[] args) {
        int[] nums1 = {1,3};
        int[] nums2 = {2,4};
        double res = findMedianSortedArrays(nums1,nums2);
        System.out.println(res);
    }
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double res  = 0;
        int[] temp = new int[nums1.length + nums2.length];
        int lpoint = 0,rpoint = 0,point = 0;
        while(lpoint < nums1.length && rpoint < nums2.length){
            if(nums1[lpoint] < nums2[rpoint]){
                temp[point] = nums1[lpoint];
                lpoint++;
            }else {
                temp[point] = nums2[rpoint];
                rpoint++;
            }
            point ++;
        }
//        System.out.println(Arrays.toString(temp));
        if(lpoint < nums1.length){
            System.arraycopy(nums1,lpoint,temp,point,nums1.length-lpoint);
        }
        if(rpoint < nums2.length){
            System.arraycopy(nums2,rpoint,temp,point,nums2.length-rpoint);
        }
//        System.out.println(Arrays.toString(temp));
        if(temp.length % 2 == 0){
            res =((double)temp[temp.length/2] + (double)temp[temp.length/2-1])/2.0;
        }else{
            res = temp[temp.length/2];
        }
        return res;
    }
}
